import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    pageNo: 1,
    list: [],
    hasNextPage: false,
    keyword: '',
    showPopup: false,
    status: 1, 
    catId: '',
    braId:'',
    catList:[],
    purchase: '',
    purchaseList: [{
      id: '',
      text: "全部"
    }, {
      id: 1,
      text: "仅限平台采购"
      }, {
        id: '0',
        text: "商城商品"
      }],
  },
  onLoad:function(){
    this.getlist() 
  },

  onShow: function() {
    // this.getlist() 

  },


  //********************筛选更多********************
  popupRight: function() {
    this.setData({
      showPopup: true
    })
    this.getCat()
  },

  popupCancel: function() {
    this.setData({
      showPopup: false
    })
  },

  popupConfirm: function() {
    this.setData({
      pageNo: 1
    })
    this.getlist();
    this.popupCancel();   
  },

  //选择状态
  checkStatus: function(e) {
    var status = e.currentTarget.dataset.id
    this.setData({
      status
    })
  },


  //选择状态
  checkpurchase: function (e) {
    var purchase = e.currentTarget.dataset.id || ''
    console.log(purchase, 'status')
    this.setData({
      purchase
    })
  },


  //选择分类
  handleCatItem: function (e) {
    var code = e.currentTarget.dataset.id;
    this.setData({
      catId: code,
    })
    this.getbrand()
  },

  handlebrand: function (e) {
    var code = e.currentTarget.dataset.id;
    this.setData({
      braId: code,
    })
  },

  //********************筛选更多end********************

  //查询列表
  getlist: function() {
    var params = {
      keyword: this.data.keyword,
      status: this.data.status,
      pageNo: this.data.pageNo,
      catId: this.data.catId,
      braId: this.data.braId
    }
    if (this.data.purchase!=''){ 
        params.purchase= this.data.purchase
    }
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + '/gds/protected/gdsMgt/findList',
      method: 'POST',
      data:params
    }, (res) => {
      let list = res.list;
      if (this.data.pageNo !== 1) {
        list = this.data.list.concat(list);
      }
      this.setData({
        list: list,
        pageNo: res.page.pageNo,
        hasNextPage: res.page.hasNextPage,
      });
    })
  },


  //下拉刷新
  onPullDownRefresh: function() {
    this.refreshData();
    wx.stopPullDownRefresh()
  },

  refreshData: function() {
    this.setData({
      pageNo: 1
    })
    this.getlist();
  },

  //触底时，加载更多
  onReachBottom: function() {
    if (this.data.hasNextPage) {
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      this.getlist();
    }
  },

  //搜索关键词
  seachInputConfirm: function(e) {
    this.setData({
      pageNo: 1,
      keyword: e.detail.value
    })
    this.getlist();
  },

  seachBtn: function () {
    this.setData({
      pageNo: 1,
      keyword: this.data.keyword
    })
    this.getlist();
  },
  
  seachInputVal: function(e) {
    var inputval = e.detail.value;
    
    this.setData({
      keyword: inputval
    })
  },

  //清空
  clearInput: function() {
    this.setData({
      keyword: ''
    })
    this.getlist();
  },



  //编辑
  handleEdit: function(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/publish/goods/goods?id=' + id,
    })
  },


  //操作方法
  handlFun: function(e) {
    var id = e.currentTarget.dataset.id;
    var operation = e.currentTarget.dataset.operation
    var title, apiUrl
    switch (operation) {
      case 'up':
        title = '上架'
        apiUrl = '/gds/protected/gdsMgt/putOnGoods'
        break;
      case 'down':
        title = '下架'
        apiUrl = '/gds/protected/gdsMgt/putDownGoods'
        break;
      case 'delect':
        title = '删除'
        apiUrl = '/gds/protected/gdsMgt/deleteGoods'
        break;
    }

    var that = this;
    wx.showModal({
      title: '提示',
      confirmText: '确定',
      cancelText: '取消',
      content: "确定" + title + "该商品",
      success: function(res) {
        if (res.confirm) {
          var params = {
            id,
            apiUrl,
            title
          }
          that.postRequest(params)
        }
      }
    })
  },



  postRequest: function(params) {
    var that = this
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + params.apiUrl,
      method: 'POST',
      data: {
        id: params.id
      }
    }, (res) => { 
      wx.showToast({
        title: params.title + "成功",
      })
      setTimeout(()=>{
        that.getlist()
      },1500)
      
    })
  },


  getCat: function () {
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + "/gds/goods/findGoodsCat",
      method: 'POST',
      data: {}
    }, (res) => {
      this.setData({
        catList: res.list
      })
      this.getbrand()
    })
  },


  //品牌
  getbrand: function () {
    _wxRequest.wxAjax({
      url: app.globalData.apiUrl + "/gds/brand/find",
      method: 'POST',
      data: {
        catId: this.data.catId
      }
    }, (res) => {
      this.setData({
        brandList: res.list
      })
    })
  },

  toGoodsDetail(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/good/detail/index?id=' + id,
    })
  },
  

})